﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MT.eVotePRS.Model
{
    public class HandsetDatas
    {
        public byte _StartByte = new byte();
        public byte _CommandCode = new byte();
        public int _StringLength = 0;
        public string _DataString = string.Empty;
        public byte _EndCode = new byte();
        public string _MacStr = string.Empty;
        public string _UserID = string.Empty;
        // ExamData 0x69
        public string _QuestionNum = string.Empty;
        public string _PaperNum = string.Empty;
        public string _Answer = string.Empty;
        // teacher send string 0x67
        public byte _CheckSum = new byte();
        public byte _SendCounter = new byte();

        // 0x13 
        public byte _LockFlag = new byte();
        public byte _BellOn = new byte();
        public byte[] _RemoteID = new byte[2];

        public byte StartByte
        {
            get { return _StartByte; }
        }
        public byte CommandCode
        {
            get { return _CommandCode; }
        }
        public int StringLength
        {
            get { return _StringLength; }
        }
        public String DataString
        {
            get { return _DataString; }
        }
        public byte EndCode
        {
            get { return _EndCode; }
        }

        public HandsetDatas(byte[] buf)
        {
            if (((buf.Length < 3) || (buf[0] == 0)) || (buf.Length < 6))
            {
            }
            _StartByte = buf[0];
            _CommandCode = buf[1];
            _StringLength = (int)buf[2];

            // Keypad Mac Address
            if (_CommandCode == 0x13 || _CommandCode == 0x67 || _CommandCode == 0x69 || _CommandCode == 0x6C)
            {
                _MacStr = string.Empty;
                for (int i = 3; i <= 5; i++)
                {
                    _MacStr = _MacStr + buf[i].ToString("X2");
                }
                try
                {
                    if (_MacStr != null && _MacStr != string.Empty)
                        _MacStr = Convert.ToString(Convert.ToInt32(_MacStr, 16));
                    while (_MacStr.Length < 8)
                    {
                        _MacStr = "0" + _MacStr;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }

            switch (_CommandCode)
            {
                case 0x13:
                    _LockFlag = buf[3 + 3];
                    _BellOn = buf[3 + 3 + 1];
                    _RemoteID[0] = buf[3 + 3 + 1 + 1];
                    _RemoteID[0] = buf[3 + 3 + 1 + 1 + 1];

                    byte[] dataDst = new byte[_StringLength - 7 + 1];
                    Buffer.BlockCopy(buf, 3 + 7, dataDst, 0, _StringLength - 7);
                    _UserID = System.Text.Encoding.ASCII.GetString(dataDst);
                    break;

                case 0x67:
                    /* Normal version Keypad
                     * byte[] dataDst1 = new byte[_StringLength - 5 + 1];
                    Buffer.BlockCopy(buf, 3 + 3, dataDst1, 0, _StringLength -5 );
                    _DataString = System.Text.Encoding.ASCII.GetString(dataDst1);*/

                    /*  2011-02-20 New version for HKUST specially. */
                    byte[] dataDst1 = new byte[_StringLength - 3 - 9 + 1];
                    Buffer.BlockCopy(buf, 3 + 3, dataDst1, 0, _StringLength - 3 - 9);
                    _DataString = System.Text.Encoding.ASCII.GetString(dataDst1);

                    dataDst1 = new byte[_StringLength - 3 - _DataString.Length + 1];
                    Buffer.BlockCopy(buf, 3 + 3 + _DataString.Length, dataDst1, 0, _StringLength - 3 - _DataString.Length);
                    _UserID = System.Text.Encoding.ASCII.GetString(dataDst1);

                    break;

                case 0x69:
                    _PaperNum = ((char)buf[7]).ToString();
                    _QuestionNum = ((char)buf[6]).ToString();
                    _Answer = ((char)buf[8]).ToString();

                    byte[] dataDst2 = new byte[_StringLength - 6 + 1];
                    Buffer.BlockCopy(buf, 3 + 3 + 3, dataDst2, 0, _StringLength - 6);
                    _UserID = System.Text.Encoding.ASCII.GetString(dataDst2);
                    break;

                case 0x6C:
                    for (int i = 3 + 3; i < 3 + _StringLength; i++)
                    {
                        if (buf[i] != 0x00)
                        {
                            _UserID = _UserID + ((char)buf[i]).ToString();
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
    
}
